package com.ztesoft.huangfu.learning.springboot.springbootrabbitmq.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * Author: huangfu.daohui
 * Date: 2018/5/27
 * Time: 20:42
 * Version: 1.0
 * Description:生产者，发送消息。消费者在RabbitmqService中
 * 参考https://www.cnblogs.com/xmzJava/p/8036591.html
 * http://www.cnblogs.com/ityouknow/p/6120544.html
 * https://www.xncoding.com/2017/08/06/spring/sb-rabbitmq.html
 **/
@RestController
@RequestMapping("/rabbitmq")
@Slf4j
public class RabbitmqController {

    @Autowired
    private AmqpTemplate amqpTemplate;

    // 简单的队列模式
    //http://localhost:8080/rabbitmq/helloSend 控制台可看到消息的首发
    @GetMapping("/helloSend")
    public void helloSend() {
        String context = "hello " + new Date();
        log.info("Sender:" + context);
        this.amqpTemplate.convertAndSend("hello", context);
    }

    //通过这个例子我们可以看做高并发情况下的消息产生和消费，这会产生一个消息丢失的问题。
    // 万一客户端在处理消息的时候挂了，那这条消息就相当于被浪费了，针对这种情况，rabbitmq推出了消息ack机制
    @GetMapping("/neosend")
    public void neosend(int i) {
        String context = "spirng boot neo queue" + " ****** " + i;
        log.info("Sender1 : " + context);
        this.amqpTemplate.convertAndSend("neo", context);
    }


    // 发布订阅模式
    @GetMapping("/fanoutsend")
    public void send() {
        String context = "hi, fanout msg ";
        System.out.println("Sender : " + context);
        this.amqpTemplate.convertAndSend("fanoutExchange", "", context);
    }
}
